/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 				</p>
 * <p>		    All right reserved. 		   			</p>
 * <p>项目名称 ：深圳公路信息资源整合及国省检日常化监管管理        				</p>
 * <p>创建者 ：hey										</p>
 * <p>描   述  ：路政管理 - 执法监督 - 举报渠道畅通			 			</p>
 * <p>最后修改 ：$: 2017年9月4日-上午10:35:21 v 1.0.0	 hey   $   </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.lzgl.enforSup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.lzgl.ReportChannelsDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.ReportChannels;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.enforSup.RepChanUnimpedService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.StringUtils;


@Service
@Transactional
public class RepChanUnimpedServiceImpl implements RepChanUnimpedService {
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private ReportChannelsDao reportChannelsDao;
	
	@Override
	public List<ReportChannels> getReportChannelsList(QueryFilter queryFilter){
		return reportChannelsDao.find(queryFilter);
	}
	
	@Override
	public Map<String, Object> getDevelopPlanmMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<ReportChannels> reChannelsList = new ArrayList<ReportChannels>();
		List<Object> value = new ArrayList<Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		
		//公布信息完整得分
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//随机拨打4次电话，均有人及时接听得分
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//随机抽查两次投诉记录，均予以答复的得分
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		
		//投诉记录id
		String repoortId = (String) jsonObject.get("reportChannelsId");
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		double total =	yearFull + yearPart; 					//检查得分
		double percentage =0; 									//系统完善度
		double tableFull  = yearFull + yearPart + reserveMark;	//满分
		
		boolean flag = false;
		double tempScore=0;
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isEmpty(repoortId)) {
			reChannelsList = this.getReportChannelsList(queryFilter);
		} else {
			value.add(repoortId);
			flag = true;
			queryFilter.addFilter("id", repoortId, MatchType.EQ);
			reChannelsList = this.getReportChannelsList(queryFilter);
			
			//1表示信息公布完整，2表示信息公布不完整
			int temp = 0;
			for (ReportChannels reportChannels : reChannelsList) {
				temp += "1".equals(reportChannels.getIsReply()) ? 1 : 0;
			}
			
			if (temp >= 2) {
				total += reserveMark;
				tempScore += reserveMark;
			}
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, value);
		map.put("percent", percentage);
		map.put("att", list);
		map.put("tempScore", tempScore);
		map.put("flag", flag);
		map.put("reChannelsList", reChannelsList);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
